package com.study.dao;
import com.study.bean.Telephone_new;
import com.study.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class Telephone_newDaoImpl1 implements Telephone_newDao {
    @Override
    public int add(Telephone_new t) throws SQLException {
        String sql = "insert into `student` (`sname`,`sage`,`sex`) values (?,?,?)";

        Object[] para = {t.getName(),t.getAge(),t.getGender()};

        QueryRunner queryRunner = new QueryRunner();

        int update = queryRunner.update(JdbcUtil.getConnection(),sql, para);


        return 0;
    }

    @Override
    public int del(Integer id) throws SQLException {
        String sql  = "delete from `student` where  `id` = ? ";

        Object[]  para = {id};

        QueryRunner queryRunner = new QueryRunner();

        int update = queryRunner.update(JdbcUtil.getConnection(),sql, para);


        return 0;
    }

    @Override
    public int update(Telephone_new t) throws SQLException {
        String sql = "update `student`  set `name` = ? ,`age` = ? , `sex` = ? where id = ?";

        Object[]  para = {t.getName(),t.getGender(),t.getAddr(),t.getId()};

        QueryRunner queryRunner = new QueryRunner();
        int update = queryRunner.update(JdbcUtil.getConnection(),sql, para);


        return 0;
    }

    @Override
    public List<Telephone_new> seletByName(String name) throws SQLException {
        String sql = "select * from `student` where `name` = ? ";
        Object[] para = {name};

        QueryRunner queryRunner = new QueryRunner();

        List<Telephone_new> query = queryRunner.query(JdbcUtil.getConnection(),sql, new BeanListHandler<Telephone_new>(Telephone_new.class), para);


        return query;
    }

    @Override
    public List<Telephone_new> selectAll() throws SQLException {
        return null;
    }

    @Override
    public Telephone_new selectById(Integer id) throws SQLException {
        String sql = "select * from `student` where `id` = ? ";
        Object[] para = {id};
        QueryRunner queryRunner = new QueryRunner();
        Telephone_new query = queryRunner.query(JdbcUtil.getConnection(),sql, new BeanHandler<>(Telephone_new.class), para);
        return query;
    }
}
